package com.jcl.cloud.services.unicrawler.controller;


import com.alibaba.fastjson.JSON;
import com.jcl.cloud.common.service.exception.JclApiException;
import com.jcl.cloud.common.service.exception.JclCrawlException;
import com.jcl.cloud.services.gateway.common.task.BuyResumeReq;
import com.jcl.cloud.services.gateway.common.task.BuyResumeRsp;
import com.jcl.cloud.services.unicrawler.common.dto.*;
import com.jcl.cloud.services.unicrawler.service.CrawlerManageService;
import com.jcl.cloud.services.unicrawler.service.CrawlerTaskService;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(
        value = {"/union-crawler/api/v1"},
        produces = {"application/json"}
)
public class TaskController {
    private static final Logger logger = LoggerFactory.getLogger(TaskController.class);
    @Autowired
    private CrawlerManageService crawlerManageService;

    @Autowired
    private CrawlerTaskService crawlerTaskService;

    @RequestMapping(
            value = {"/manager/tasks"},
            method = {RequestMethod.POST}
    )
    public UniCrawlTaskRsp createTask(@RequestBody CrawlTaskMgrReq taskMgrReq) throws JclApiException {

        UniCrawlTaskRsp uniCrawlTaskRsp = null;
        try {
            MDC.put("taskId",taskMgrReq.getTaskId());
            CrawlTask crawlTask = this.crawlerManageService.createCrawlTask(taskMgrReq);
            uniCrawlTaskRsp = new UniCrawlTaskRsp();
            uniCrawlTaskRsp.setCrawlerId(crawlTask.getCrawlerId());
        } catch (JclCrawlException e) {
            logger.error("Create Task Failed : {}", e.getMessage());
            throw new JclApiException();
        } finally {
            this.clearMdcData();
        }

        return uniCrawlTaskRsp;
    }


    @RequestMapping(
            value = {"/tasks/grab"},
            method = {RequestMethod.POST}
    )
    public GrabResult grabTask(@RequestBody GrabRequest grabReq) {
        GrabResult grabResult = null;
        try {
            grabResult = this.crawlerTaskService.grabTask(grabReq);
        } catch (JclCrawlException e) {
            e.printStackTrace();
        } finally {

        }


        return grabResult;
    }



    @RequestMapping(
            value = {"/tasks/buy/resume"},
            method = {RequestMethod.POST}
    )
    public BuyResumeRsp buyResume(@RequestBody BuyResumeReq buyResumeReq){

        BuyResumeRsp buyResumeRsp = new BuyResumeRsp();

        try {
            logger.info("union-crawler获取到的请求信息为：" + JSON.toJSONString(buyResumeReq));
            buyResumeRsp =  this.crawlerManageService.buyResume(buyResumeReq);


        } catch (JclCrawlException e) {
            e.printStackTrace();
        }

        return buyResumeRsp;
    }




    protected void clearMdcData() {
        MDC.remove("taskId");
        MDC.remove("tenantId");
        MDC.remove("accMD5");
        MDC.remove("crawlerId");
    }




}
